package src.Thirteen;

import javax.swing.*;
import java.awt.*;

public class JListTest extends JFrame {

    public JListTest(){
        Container cp = getContentPane();
        cp.setLayout(null);
        JList jList = new JList(new MyListModel());
        JScrollPane jScrollPane = new JScrollPane();
        jScrollPane.setViewportView(jList);
        jScrollPane.setBounds(10,10,100,100);
        cp.add(jList);

        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(500,500);
    }

    public static void main(String[] args) {
        JListTest jListTest = new JListTest();
    }

    class MyListModel extends AbstractListModel{

        private String[] contents = {"列表1","列表2","列表3","列表4","列表5","列表6"};


        @Override
        public int getSize() {
            return contents.length;
        }

        @Override
        public Object getElementAt(int index) {
            if(index < contents.length){
                return contents[index++];
            }else {
                return null;
            }

        }
    }
}
